import { NameSpace } from 'utils';
import exceed from 'utils/apimap';
import { Message } from '@teamix/ui';
import intl from '../../../../../../../../../locale';

const { id, organization_id } = window.FORCE.organization;

const ns = NameSpace('organizationRepositoryPermissions');
export const RECEIVE_DATA = ns('RECEIVE_DATA');
export const SET_DATA = ns('SET_DATA');
export const GET_ORGALIASNAME = ns('GET_ORGALIASNAME');
export const UPDATE_DATA = ns('UPDATE_DATA');

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

// 查询企业标识名设置
export function getOrgAliasName() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.alias.name.get',
        params: {
          orgId: organization_id,
        },
      })
      .then((result) => {
        dispatch({
          type: GET_ORGALIASNAME,
          data: result,
        });
      });
  };
}

export function checkOrgAliasName(value, cb) {
  return (dispatch) => {
    exceed
      .fetch({
        api: 'organization.verify',
        data: value,
      })
      .then(() => {
        dispatch(setOrgAliasName(value));
        cb && cb();
      })
      .fail((err) => {
        const errCause = JSON.parse(err.responseText).message;
        console.log(errCause);
      });
  };
}

export function setOrgAliasName(value) {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.alias.name.update',
        params: {
          id,
        },

        data: value,
      })
      .then(() => {
        Message.success({
          title: intl.get({
            id: 'code-assets.customize.actions.TheIdentifierIsSet',
            defaultMessage: '标识名设置成功',
          }),
        });

        dispatch(getOrgAliasName());
      });
  };
}
